package guowei.gulistorage.storage.listener;

import com.rabbitmq.client.Channel;
import guowei.common.to.mq.StockLockedTo;
import guowei.gulistorage.storage.feign.OrderFeign;
import guowei.gulistorage.storage.service.WareOrderTaskDetailService;
import guowei.gulistorage.storage.service.WareOrderTaskService;
import guowei.gulistorage.storage.service.WareSkuService;
import guowei.common.to.mq.OrderVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class stockListener {

    @Autowired
    WareSkuService wareSkuService;
    @Autowired
    WareOrderTaskDetailService wareOrderTaskDetailService;

    @Autowired
    WareOrderTaskService wareOrderTaskService;

    @Autowired
    OrderFeign orderFeign;

    /**
     * 监听消息 解锁库存    这是解锁库存辅消息
     */
    @RabbitHandler
    public void listener(StockLockedTo stockLockedTo, Message message, Channel channel) throws IOException {
        System.out.println("storage辅助解锁库存");
        try {
            wareSkuService.listenStock(stockLockedTo, message, channel);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }
    }
    @RabbitHandler
    public void listenerOrderStock(OrderVo orderVo,Message message,Channel channel) throws IOException {
        System.out.println("接收到了order服务发来的主解锁库存信息");
        try {
            wareSkuService.unlockStock(orderVo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}